<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');

class Edit extends Layout {

    protected $objAlert, $mctUser;
    protected $txtFirstName, $txtLastName, $txtPhone, $txtMobile, $txtEmail, $chkIsEnable, $txtUsername, $txtPassword;
    protected $btnSave;

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/user/menu/edit.tpl.php';

        $this->CssClass = 'form-horizontal';

        $this->objAlert = new Alert($this);

        $this->mctUser = UserMetaControl::CreateFromQueryString($this, QMetaControlCreateType::CreateOrEdit);

        $this->txtFirstName = $this->mctUser->txtFirstName_Create();
        $this->txtLastName = $this->mctUser->txtLastName_Create();
        $this->txtPhone = $this->mctUser->txtPhone_Create();
        $this->txtMobile = $this->mctUser->txtMobile_Create();
        $this->txtEmail = $this->mctUser->txtEmail_Create();

        $this->chkIsEnable = $this->mctUser->chkIsEnable_Create();
        $this->chkIsEnable->AddCssFile('../../../admin/content/plugins/jquery-prettyCheckable/dist/prettyCheckable.css');
        $this->chkIsEnable->AddJavascriptFile('../../../admin/content/plugins/jquery-prettyCheckable/dist/prettyCheckable.min.js');

        $this->txtUsername = $this->mctUser->txtUsername_Create();
        $this->txtPassword = $this->mctUser->txtPassword_Create();

        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save Changes');
        $this->btnSave->PrimaryButton = $this->btnSave->CausesValidation = true;
        $this->btnSave->AddAction(new QClickEvent(), new QToggleEnableAction($this->btnSave));
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));

        $this->btnSave->AddJavascriptFile('../../../admin/content/js/user/edit.js');
    }

    protected function Form_PreRender() {
        parent::Form_PreRender();

        QApplication::ExecuteJavaScript('Edit.init();');
    }

    protected function Form_Validate() {
        $blnToReturn = parent::Form_Validate();

        if ($blnToReturn)
            $this->btnSave->Enabled = true;

        return $blnToReturn;
    }

    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
        try {
            if (!$this->mctUser->EditMode)
                $this->mctUser->User->LogInsert = QDateTime::Now();

            $this->mctUser->User->IsAdmin = false;
            $this->mctUser->User->LogUser = $this->objCurrentUser->Username;
            $this->mctUser->SaveUser();

            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate('User Saved Successful !'));
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }

        $this->btnSave->Enabled = true;
    }

}

Edit::Run('Edit', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/user/edit.tpl.php');
?>